import { defineConfig, devices } from '@playwright/test';
import { resolve } from 'path';

export default defineConfig({
  // 测试目录
  testDir: './tests/e2e',

  // 完全并行运行测试
  fullyParallel: true,

  // 在CI环境中禁止只运行失败的测试
  forbidOnly: !!process.env.CI,

  // CI环境重试次数
  retries: process.env.CI ? 2 : 0,

  // 工作进程数量
  workers: process.env.CI ? 1 : undefined,

  // 报告器配置
  reporter: [
    ['html', { outputFolder: 'playwright-report' }],
    ['json', { outputFile: 'test-results.json' }],
    ['list'],
    ['junit', { outputFile: 'test-results/junit-results.xml' }]
  ],

  // 全局测试配置
  use: {
    // 基础URL - 根据环境变量设置
    baseURL: process.env.PLAYWRIGHT_BASE_URL || 'http://localhost:3001',

    // 截图配置
    screenshot: 'only-on-failure',

    // 视频录制
    video: 'retain-on-failure',

    // 追踪配置
    trace: 'on-first-retry',

    // 超时设置
    timeout: 30000,

    // 动作超时
    actionTimeout: 15000,

    // 导航超时
    navigationTimeout: 30000,

    // 忽略HTTPS错误（仅用于开发环境）
    ignoreHTTPSErrors: true,

    // 测试数据收集
    collectCoverage: process.env.COLLECT_COVERAGE === 'true',
  },

  // 项目配置 - 支持多浏览器测试
  projects: [
    {
      name: 'chromium',
      use: {
        ...devices['Desktop Chrome'],
        launchOptions: {
          args: ['--start-maximized']
        }
      },
    },
    {
      name: 'firefox',
      use: {
        ...devices['Desktop Firefox'],
        launchOptions: {
          args: [
            '--start-maximized',
            '--disable-web-security',
            '--disable-features=VizDisplayCompositor',
            '--no-sandbox',
            '--disable-setuid-sandbox'
          ]
        }
      },
    },
    {
      name: 'webkit',
      use: {
        ...devices['Desktop Safari'],
        launchOptions: {
          args: ['--start-maximized']
        }
      },
    },

    // 移动设备测试
    {
      name: 'Mobile Chrome',
      use: { ...devices['Pixel 5'] },
    },
    {
      name: 'Mobile Safari',
      use: { ...devices['iPhone 12'] },
    },
  ],

  // Web服务器配置
  webServer: {
    command: process.env.PLAYWRIGHT_BUILD === 'true'
      ? 'npm run build:craco && npm run serve:craco'
      : 'npm run start:craco',
    url: process.env.PLAYWRIGHT_BASE_URL || 'http://localhost:3001',
    reuseExistingServer: !process.env.CI,
    timeout: 120000,
    stdout: 'pipe',
    stderr: 'pipe',
  },

  // 依赖项目配置
  dependencies: [
    {
      // 后端API服务器
      command: 'cd ../../backend/aspnet-core && dotnet run --project src/CKY.AgentPlatform.HttpApi.Host',
      url: 'http://localhost:6001',
      reuseExistingServer: !process.env.CI,
      timeout: 120000,
      stdout: 'pipe',
      stderr: 'pipe',
    },
  ],

  // 全局设置
  globalSetup: require.resolve('./tests/global-setup.ts'),
  globalTeardown: require.resolve('./tests/global-teardown.ts'),

  // 输出目录配置
  outputDir: 'test-results/',

  // 测试匹配模式
  testMatch: [
    '**/*.spec.ts',
    '**/*.test.ts',
    '**/*.cy.ts'
  ],

  // 忽略的文件
  testIgnore: [
    '**/node_modules/**',
    '**/dist/**',
    '**/build/**'
  ],

  // 元数据配置
  metadata: {
    testEnvironment: 'playwright',
    testRunner: '@playwright/test',
    testMatch: ['**/*.spec.ts'],
    testTimeout: 30000,
  },

  // 自定义配置
  expect: {
    timeout: 5000,
    toMatchSnapshot: {
      threshold: 0.2
    }
  }
});